const express = require(`express`);
const router = express.Router();
const fs = require("fs");
const path = require("path");
const childProcess = require("child_process");
// router.use((req, res, next) => {
//   console.log(`路由执行成功啦~~~`, Date.now());
//   next()
// })

router.post(`/space`, (req, res, next) => {
    console.log(req.body);
    let fridends = req.body.phone;
    if (!fridends) {
        res.send("请输入手机号");
    }
    let list = fridends.split("\n");
    list = list.filter((item) => item);

    let content = "";

    let ten = 0;
    list.forEach((phone, index) => {
        if (index % 10 == 0) {
            ten += 1;
            content += `
            第${ten}批：
            `;
        }
        content +=
            `${index + 1}     ` +
            phone +
            `
        `;
    });
    res.json(content);
});

router.post(`/vcf`, (req, res, next) => {
    let fridends = req.body.phone;
    if (!fridends) {
        res.send("请输入手机号");
    }
    let list = fridends.split("\n");
    list = list.filter((item) => item);

    let fileContent = "";

    list.forEach((phone) => {
        fileContent += `BEGIN:VCARD
VERSION:3.0
item0.X-ABLabel:工作
item0.ORG:兼职;
item1.NOTE:用完删除
item1.X-ABLabel:描述
EMAIL;TYPE=HOME,INTERNET,pref:594341275@qq.com
FN:1-${phone}
N:;1-${phone};;;
TEL;TYPE=VOICE,CELL,pref;VALUE=text:${phone}
UID:58AC03F7-C95B-4D4C-9F59-965396333785
END:VCARD`;
    });
    console.log(fileContent);
    const phonePath = path.resolve(__dirname, "../../dist/phone.vcf");
    fs.writeFileSync(phonePath, fileContent);

    res.json({
        url: "http://localhost:8080/phone.vcf",
    });
});

module.exports = router;
